import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 刘柄华
 * Date: 2022-05-22
 * Time: 18:34
 */
public class TestDemo03 {
    /*
    *从尾到头打印链表   递归
    * */


    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        ArrayList<Integer> list = new ArrayList<>();
        if(listNode==null)return list;
        printListFromTailToHeader(listNode,list);
        return list;
    }
    public void printListFromTailToHeader(ListNode listNode,ArrayList list){
        if(listNode==null)return;
        printListFromTailToHeader(listNode.next,list);
        list.add(listNode.val);
    }

    public class ListNode {
        int val;
        ListNode next = null;

        ListNode(int val) {
            this.val = val;
        }
    }
}
